﻿using System;
using System.Net;
using System.IO;
using System.Windows.Forms;
using System.Net.Sockets;

class IJJI_Login
{
    private string launchString = string.Empty;
    public bool launchStringValid()
    {
        if (launchString == null)
        {
            return false;
        }
        if (launchString != null)
        {
            return true;
        }
        return false;
    }
    private bool loggedIn = false;
    private CookieContainer cookies = new CookieContainer();
    public string authenticate(string username, string password)
    {
        HttpWebRequest htreq = (HttpWebRequest)WebRequest.Create("http://auth.ijji.com/auth.nhn?memberid=" + username + "&password=" + password);
        htreq.CookieContainer = cookies;
        HttpWebResponse htres = (HttpWebResponse)htreq.GetResponse();
        StreamReader htread = new StreamReader(htres.GetResponseStream());
        while (!loggedIn)
        {
            string readline = htread.ReadLine();
            MessageBox.Show(readline);
            if (readline == "-1|Memeber ID is not found")
            {
                MessageBox.Show("Account not found!", "Error");
                return readline;
            }
            if (readline == "-2|You have a wrong password")
            {
                MessageBox.Show("Incorrect password!", "Error");
                return readline;
            }
            if (readline == "-3|Memeber ID has been terminated")
            {
                MessageBox.Show("This account no-longer exists!", "Error");
                return readline;
            }
            if (readline == "-4|Member ID has a penalty")
            {
                MessageBox.Show("This account is banned", "Error");
                return readline;
            }
            if (readline.StartsWith("1"))
            {
                loggedIn = true;
                return readline;
            }
        }
        return null;
    }
    private string getLaunchString(string request)
    {
        string ret = null;
        try
        {
            TcpClient tcpClient = new TcpClient("gstr.ijjimax.com", 10000);
            StreamReader readTcp = new StreamReader(tcpClient.GetStream());
            StreamWriter writeTcp = new StreamWriter(tcpClient.GetStream());
            while (!tcpClient.Connected) ;
            writeTcp.WriteLine(
                                "GET " +
                                request +
                                " HTTP/1.1\r\n" +
                                "host:gstr.ijjimax.com:10000\r\n" +
                                "User-Agent: Microsoft Internet Explorer\r\n" +
                                "Pragma: no-cache\r\n" +
                                "\r\n"
                              );
            writeTcp.Flush();
            ret = readTcp.ReadLine();
            tcpClient.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return ret;
    }

    public void launchGunz()
    {
        HttpWebRequest htreq = (HttpWebRequest)WebRequest.Create("http://gunz.ijji.com/common/launch.nhn?gameId=u_gunz");
        htreq.CookieContainer = cookies;
        HttpWebResponse htres = (HttpWebResponse)htreq.GetResponse();
        StreamReader htread = new StreamReader(htres.GetResponseStream());
        string readline = htread.ReadLine();
        if (readline.Contains("\"isLogin\":true"))
        {
            launchString = (string)readline.Split('"')[13];//Now contains: launchscript:
            launchString = launchString.Remove(0, 10);
            launchString = launchString.Remove(launchString.Length - 2);
            launchString = launchString.Split('?')[1];
            launchString = "\\?" + launchString;
            launchString = getLaunchString(launchString);
            try
            {
                System.Diagnostics.Process.Start("C:\\IJJI\\ENGLISH\\Gunz\\Gunz.exe", launchString);
            }
            catch (Exception)
            {
                MessageBox.Show("Gunz is not installed or is generally missing, please install it.");
                return;
            }
        }
    }
}